//
//  ViewController.m
//  A01- 团购
//
//  Created by Rainy Zou on 15/11/22.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "ViewController.h"
#import "CZGroupBuying.h"
#import "GZGroupBuyingCell.h"
#import "CZFooterView.h"
#import "CZHeaderView.h"

@interface ViewController ()<UITableViewDataSource,CZFooterViewDelegate>
@property (nonatomic,strong) NSMutableArray *groupBuyings;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

//1 懒加载
- (NSArray *)groupBuyings{
    if(_groupBuyings == nil){
        _groupBuyings = [CZGroupBuying groupBuyingsList];
    }
    return _groupBuyings;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置行高
    self.tableView.rowHeight = 80;
    
    
    //设置table 底部
    CZFooterView *footerView = [CZFooterView footerView];
    
    footerView.delegate = self;
    
    self.tableView.tableFooterView = footerView;

    //设置table头部
    CZHeaderView *headerView = [CZHeaderView headerView];
    self.tableView.tableHeaderView = headerView;
}

#pragma mark  -- tableview 数据源的方法

//返回每组多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groupBuyings.count;
}

//返回每行显示的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GZGroupBuyingCell *cell = [GZGroupBuyingCell groupBuyingCellWithTableView:tableView];
    cell.groupBuying = self.groupBuyings[indexPath.row];
    
    return cell;
}

#pragma mark footView 的代理方法
- (void)footerViewDidClickedLoadMoreBtn:(CZFooterView *)footerView
{
    CZGroupBuying *gd = [[CZGroupBuying alloc] init];
    gd.title = @"牛腩王";
    gd.price = @"15";
    gd.buyCount=@"100";
    gd.icon = @"2c97690e72365e38e3e2a95b934b8dd2.png";
    [self.groupBuyings addObject:gd];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.groupBuyings.count -1 inSection:0];
    
    
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    
    //错误的加载，不能rows的变化
//    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
    //性能存在问题
//    [self.tableView  reloadData];
}

@end
